﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PongTeam
{
    public class Balle : Sprite
    {
        private int _screenHeight;
        private int _screenWidth;

        private float _rapportWidth;
        private float _rapportHeight;



        public Balle(int screenWidth, int screenHeight)
        {
            _rapportWidth = screenWidth / 1920.0f;
            _rapportHeight = screenHeight / (screenHeight / (float)screenWidth * 1920.0f);
            _screenHeight = screenHeight;
            _screenWidth = screenWidth;
        }

        public override void Initialize(int screenWidth, int screenHeight)
        {
            base.Initialize(screenWidth, screenHeight);
            Random rand = new Random();
            int n = rand.Next(4);
            switch (n)
            {
                case 0: Direction = new Vector2(1, 1); break;
                case 1: Direction = new Vector2(1, -1); break;
                case 2: Direction = new Vector2(-1, 1); break;
                case 3: Direction = new Vector2(-1, -1); break;
            }
            Speed = 0.5f;
        }

        public override void LoadContent(ContentManager content, string assetName)
        {
            base.LoadContent(content, assetName);
            Position = new Vector2(_screenWidth / 2 - Texture.Width * _rapportWidth / 2, _screenHeight / 2 - Texture.Height * _rapportHeight / 2);

        }

        public void Update(GameTime gameTime, Rectangle raquetteGauche, Rectangle raquetteDroite, SoundEffect sound2)
        {
            if ((Position.Y < _screenHeight * 0.12 && Direction.Y < 0)
                || (Position.Y > _screenHeight - Texture.Height * _rapportHeight - _screenHeight * 0.12 && Direction.Y > 0))
            {
                Direction = new Vector2(Direction.X, -Direction.Y);
            }

            if ((Direction.X < 0 && raquetteGauche.Contains((int)Position.X, (int)(Position.Y + Texture.Height * _rapportHeight / 2)))
                || (Direction.X > 0 && raquetteDroite.Contains((int)(Position.X + Texture.Width * _rapportWidth), (int)(Position.Y + Texture.Height * _rapportHeight / 2))))
            {
                Direction = new Vector2(-Direction.X, Direction.Y);
                Speed += 0.03f;
                sound2.Play();
            }

            base.Update(gameTime);
        }

    }
}